package SC12Project.pm2.search;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import org.uddi.api_v3.BindingDetail;
import org.uddi.api_v3.BindingTemplate;
import org.uddi.api_v3.BusinessService;
import org.uddi.api_v3.GetBindingDetail;
import org.uddi.api_v3.GetServiceDetail;
import org.uddi.api_v3.ServiceDetail;
import org.uddi.api_v3.ServiceList;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIInquiryPortType;

import SC12Project.pm2.publish.JuddiClient;

/**
 * @author Manoj Note ServiceInfo has been updated -- not the same as provided for the exercise.. New field name has
 *         been added with getter and setter
 */
public class WSSearchEngineImpl
    implements WSSearchEngine
{

    @Override
    public List<ServiceInfo> search( String keywords )
    {
        ArrayList<ServiceInfo> publishedServiceInfoList = new ArrayList<ServiceInfo>();
        JuddiClient juddiClient = new JuddiClient();
        juddiClient.initilize();
        UDDIInquiryPortType inquiry = juddiClient.getInquiry();
        try
        {
            ServiceList list_of_services = juddiClient.findServices( "%" );
            for ( org.uddi.api_v3.ServiceInfo serviceInfo : list_of_services.getServiceInfos().getServiceInfo() )
            {
                GetServiceDetail getServiceDetail = new GetServiceDetail();
                String serviceKey = serviceInfo.getServiceKey();
                // System.out.println(serviceKey);
                getServiceDetail.getServiceKey().add( serviceKey );
                ServiceDetail serviceDetail = inquiry.getServiceDetail( getServiceDetail );
                BusinessService businessService = serviceDetail.getBusinessService().get( 0 );
                String serviceName = businessService.getName().get( 0 ).getValue();
                String serviceDescription = null;
                if ( businessService.getDescription() != null && businessService.getDescription().size() > 0 )
                    serviceDescription = businessService.getDescription().get( 0 ).getValue();

                if ( matchServiceNameAndDescritpion( serviceName, serviceDescription, keywords ) )
                {
                    ServiceInfo publishedServiceInfo = new ServiceInfo();
                    publishedServiceInfo.setName( serviceName );
                    publishedServiceInfo.setDescription( serviceDescription );

                    String bindingkey =
                        businessService.getBindingTemplates().getBindingTemplate().get( 0 ).getBindingKey();
                    GetBindingDetail getBindingDetail = new GetBindingDetail();
                    getBindingDetail.getBindingKey().add( bindingkey );
                    BindingDetail bindingdetail = inquiry.getBindingDetail( getBindingDetail );
                    BindingTemplate bindingtemplate = bindingdetail.getBindingTemplate().get( 0 );
                    String accesspoint = bindingtemplate.getAccessPoint().getValue();
                    publishedServiceInfo.setUrl( accesspoint );
                    publishedServiceInfoList.add( publishedServiceInfo );
                }
            }
        }
        catch ( DispositionReportFaultMessage e )
        {
            e.printStackTrace();
        }
        catch ( RemoteException e )
        {
            e.printStackTrace();
        }
        return publishedServiceInfoList;
    }

    private boolean matchServiceNameAndDescritpion( String serviceName, String serviceDescription, String keywords )
    {
        if ( serviceName.toLowerCase().contains( keywords.toLowerCase() )
            || ( serviceDescription != null && serviceDescription.toLowerCase().contains( keywords.toLowerCase() ) ) )
            return true;
        return false;
    }
}
